package com.lwq.view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Menu;
import java.awt.MenuItem;
import java.awt.Panel;
import java.awt.PopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

import com.lwq.cocos.CCNode;
import com.lwq.cocos.ProjectInfo;
import com.lwq.cocos.ProjectInfoAdpter;
import com.lwq.controller.ProjectController;

public class LeftEditorView extends Panel {
	private JFrame mFrame = null;
	private JTree mTree = null;
	private PopupMenu mPopupMenu = null;
	private Menu mAddMenu = null;
	private MenuItem mAddSceneMenuItem = null;
	private MenuItem mRemoveMenuItem = null;
	private MenuItem mAddLayoutMenuItem = null;
	private MenuItem mAddSpriteMenuItem = null;
	private MenuItem mAddActionMenuItem = null;
	private DefaultMutableTreeNode mSelectNode = null;
	private DefaultMutableTreeNode mTreeRoot = null;
	private DefaultTreeModel TreeModel = null;
	private static LeftEditorView instance = null;

	public static LeftEditorView getInstance(JFrame frame) {
		if (instance == null) {
			instance = new LeftEditorView(frame);
		}
		return instance;
	}

	public static LeftEditorView getInstance() {
		return instance;
	}

	public LeftEditorView(JFrame frame) {
		mFrame = frame;
		init();
		setEvent();

	}

	private void init() {
		mPopupMenu = new PopupMenu();
		mAddMenu = new Menu("Add");
		mAddSceneMenuItem = new MenuItem("Add Scene");
		mAddLayoutMenuItem = new MenuItem("Add Layout");
		mAddSpriteMenuItem = new MenuItem("Add Sprite");
		mAddActionMenuItem = new MenuItem("Add Action");
		mRemoveMenuItem = new MenuItem("Remove");
		mAddMenu.add(mAddSceneMenuItem);
		mAddMenu.add(mAddLayoutMenuItem);
		mAddMenu.add(mAddSpriteMenuItem);
		mAddMenu.add(mAddActionMenuItem);
		mAddMenu.setEnabled(true);
		mPopupMenu.setEnabled(true);
		mPopupMenu.add(mAddMenu);
		mPopupMenu.add(mRemoveMenuItem);
		// mPopupMenu.setLightWeightPopupEnabled(false);
		add(mPopupMenu);
		setEnabled(true);
		setVisible(true);

	}

	private void setEvent() {
		mAddSceneMenuItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ProjectController.projectAddSceneCommand();
			}
		});
		mAddLayoutMenuItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				ProjectController.projectAddLayoutCommand();
			}
		});
		mAddSpriteMenuItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				ProjectController.projectAddSpriteCommand();
			}
		});
		mAddActionMenuItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ProjectController.projectAddActionCommand();
			}
		});
		mRemoveMenuItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ProjectController.projectRemoveCommand();
			}
		});
	}

	private void loadNodeData(DefaultMutableTreeNode treeNode, CCNode node) {
		List<CCNode> list = null;
		CCNode mNode = null;
		DefaultMutableTreeNode childTreeNode = null;
		if (node != null) {
			list = node.getChilds();
			for (int i = 0; i < list.size(); i++) {
				mNode = list.get(i);
				childTreeNode = new DefaultMutableTreeNode(mNode.getTreeName());
				treeNode.add(childTreeNode);
				ProjectInfoAdpter.getInstance().nodesMapAddItem(childTreeNode,
						mNode);
				loadNodeData(childTreeNode, mNode);
			}
		}
	}

	private void loadData() {

		ProjectInfo inform = ProjectInfoAdpter.getInstance().getProInfo();
		List list = inform.getChilds();
		CCNode mNode = null;
		DefaultMutableTreeNode childTreeNode = null;
		if (inform != null && list != null) {
			setRoot(inform.getProjectName());
			for (int i = 0; i < list.size(); i++) {
				mNode = (CCNode) list.get(i);
				childTreeNode = new DefaultMutableTreeNode(mNode.getTreeName());
				ProjectInfoAdpter.getInstance().nodesMapAddItem(childTreeNode,
						mNode);
				mTreeRoot.add(childTreeNode);
				loadNodeData(childTreeNode, mNode);
			}
		}
		refreshScreen();
	}

	public DefaultMutableTreeNode getTreeRoot() {
		return mTreeRoot;
	}

	public void reLoadData() {
		if (mTree != null) {
			remove(mTree);
		}
		loadData();
	}

	public void TreeReLoadData() {
		if (TreeModel != null) {
			TreeModel.reload();
		}
	}

	public void removeNode(DefaultMutableTreeNode rmNode) {
		if (rmNode != mTreeRoot) {
			((DefaultMutableTreeNode) rmNode.getParent()).remove(rmNode);
		}
	}

	public void setRoot(String root) {
		if (root == null)
			return;
		mTreeRoot = new DefaultMutableTreeNode(root);
		TreeModel = new DefaultTreeModel(mTreeRoot);
		mTree = new JTree(TreeModel);
		mTree.setBackground(new Color(0xf2f2f3));
		mTree.setEnabled(true);
		mTree.setVisible(true);
		mTree.setEditable(true);
		ProjectInfoAdpter.getInstance().setRoot(mTreeRoot);
		add(mTree);
		mTree.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				if (e.isMetaDown()) {
					mPopupMenu.show(e.getComponent(), e.getX(), e.getY());
				}
				refreshScreen();
			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub

			}
		});

		mTree.addTreeSelectionListener(new TreeSelectionListener() {
			public void valueChanged(TreeSelectionEvent evt) {
				mSelectNode = (DefaultMutableTreeNode) mTree
						.getLastSelectedPathComponent(); // 返回最后选中的结点
				ProjectController.TreeSelectNodeCommand(mSelectNode);
			}
		});

		refreshScreen();
	}

	public void refreshScreen() {
		Graphics g = mFrame.getGraphics();
		mFrame.paintAll(g);
		g.dispose();
	}

}
